﻿#include "injectortablemodeldelegate.h"
#include "../../include/ui_utility.h"

InjectorTableModelDelegate::InjectorTableModelDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{

}

InjectorTableModelDelegate::~InjectorTableModelDelegate()
{

}

void InjectorTableModelDelegate::paint(QPainter *painter,
                                       const QStyleOptionViewItem &option,
                                       const QModelIndex &index) const
{
    if(index.column() == 0)
    {
        int status = index.model()->data(index, Qt::DisplayRole).toInt();
        QLabel statusLabel;
        statusLabel.setAlignment(Qt::AlignCenter);
        if(status == 0)
        {
            statusLabel.setText(QStringLiteral("正常"));
            statusLabel.setStyleSheet("font-weight:bold;color:rgb(146,200,108);background-color:#ffffff;");
        }
        else
        {
            statusLabel.setText(QStringLiteral("离线"));
            statusLabel.setStyleSheet("font-weight:bold;color:rgb(255,100,127);background-color:#ffffff;");
        }
        statusLabel.resize(option.rect.size());
        painter->save();
        painter->translate(option.rect.topLeft());
        statusLabel.render(painter);
        painter->restore();

    }
    else if(index.column() == 2 || index.column() == 3)
    {

        QProgressBar renderer;
        int progressPercentage = index.model()->data(index, Qt::DisplayRole).toInt();

        // Customize style using style-sheet..

        QString style = read_style(":/qss/rc/progressBar.qss");

        renderer.resize(option.rect.size());
        renderer.setMinimum(0);
        renderer.setMaximum(100);
        renderer.setValue(progressPercentage);

        renderer.setStyleSheet(style);
        painter->save();
        painter->translate(option.rect.topLeft());
        renderer.render(painter);
        painter->restore();
/*
        int value = index.model()->data(index).toInt();
        QStyleOptionProgressBarV2 progressBarOption;
        progressBarOption.styleObject
        progressBarOption.rect = option.rect;
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.textAlignment = Qt::AlignCenter;
        progressBarOption.textVisible = true;
        progressBarOption.progress = value;
        progressBarOption.text = tr("%1%").arg(progressBarOption.progress);
        QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
    */
     }
    else
    {
         QStyledItemDelegate::paint(painter, option, index);
    }
}
/**/
